﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GSLMS.Entity;
using GSLMS.Core;
using GSLMS.Entity.Enum;

namespace GSLMS.WebAPP.Areas.DianDang.Controllers.FKAudit.RiskControl
{
    public class RiskControlController : AdminBaseController
    {
        //
        // GET: /DianDang/RiskControl/
        protected override string navTabId
        {
            get { return "DianDang_FKAudit_RiskControl"; }
        }


         [HttpGet]
        public ActionResult Index()
        {
            
            var searchParam = new TB_APP_PROINFO();
            searchParam.RiskAuditStatus = 1;
            var model = serviceProInfo.GetAduRiskList(searchParam);
            ViewData["RowCount"] = searchParam.RowCount;
            InitSelect();
            ViewData["SearchParma"] = searchParam;
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            var searchParam = base.SearchPara<TB_APP_PROINFO>(form, new TB_APP_PROINFO());
            var model = serviceProInfo.GetAduRiskList(searchParam);
            ViewData["RowCount"] = searchParam.RowCount;
            InitSelect();
            return View(model);
        }


        //
        // GET: /DianDang/RiskControl/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /DianDang/RiskControl/Create
        public ActionResult CreateUnHouInfo(int id)
        {
            var model = serviceProInfo.GetProRiskControlReportInfo(id);


            if (model.AduRiskControl.AUDITBY == null || model.AduRiskControl.AUDITBY.Trim() == "")
            {
                model.AduRiskControl.AUDITTIME = DateTime.Now;
                model.AduRiskControl.AUDITBY = CurentUser.UserName;
            }
            if (model.AduRiskControl.PROID == 0)
            {
                model.AduRiskControl.PROID = model.ID;
            }

            return View(model);
        }

        //
        // POST: /DianDang/RiskControl/Create

        [HttpPost]
        [ValidateInput(false)] 
        public string CreateUnHouInfo(FormCollection collection)
        {
            string result = string.Empty;
            var model = new TB_APP_PROINFO();
            TryUpdateModel(model, collection);
            try
            {
                if (model.AduRiskControl != null)
                {
                    model.AduRiskControl.AUDITBY = CurentUser.UserName;
                    model.AduRiskControl.AUDITTIME = DateTime.Now;
                }

                model.AduRiskControl.STATUS = Request["hidActiondo"] == null ? 0 : Core.Helper.ToInt(Request["hidActiondo"]);

                bool isSumbit = false;
                isSumbit = serviceProInfo.AddProRiskControlReportInfo(model);

                if (isSumbit)
                { result = AjaxResultSucess; }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }

        public ActionResult CreateHouInfo(int id)
        {
            InitSelect();
            var model = serviceProInfo.GetByID(id);
            ViewData["riskstatus"] = Request["riskstatus"] == null ? 0 : Core.Helper.ToInt(Request["riskstatus"]);
            return View(model);
        }

        [HttpPost]
        public string CreateHouInfo(FormCollection collection)
        {
            string result = string.Empty;
            var model = new TB_APP_PROINFO();
            var riskAuditModel = new TB_ADU_RISKCONTROL();
            TryUpdateModel(model, collection);
            try
            {
                model.ProForHouInfo.REGINFOID = model.REGINFOID;
                riskAuditModel.STATUS = Request["hidActiondo"] == null ? 0 : Core.Helper.ToInt(Request["hidActiondo"]);
                riskAuditModel.PROID = model.ID;
                riskAuditModel.AUDITBY = CurentUser.UserName;
                riskAuditModel.AUDITTIME = DateTime.Now;
                model.AduRiskControl = riskAuditModel;

                model.AduRiskControlCust = null;
                model.AduRiskControlPro = null;

                bool isSumbit =false;
                if (model.ID > 0)
                    isSumbit = serviceProInfo.Update(model);

                if (isSumbit)
                { result = AjaxResultSucess; }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }


        //
        // GET: /DianDang/RiskControl/Edit/5

        public ActionResult Loger(int id)
        {
            var model = serviceProInfo.GetByID(id);
            return View(id);
        }

        //
        // POST: /DianDang/RiskControl/Edit/5

        [HttpPost]
        public string Loger(int id, FormCollection collection)
        {
            string result = string.Empty;
            try
            {
                bool isSumbit = false;
                if (isSumbit)
                { result = AjaxResultSucess; }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }

        protected void InitSelect()
        {
            ViewData["SignMan"] = new SelectList(serviceEmp.GetAllEmp(), "ID", "empname");
            ViewData["Product"] = new SelectList(serviceSysProduct.GetBySysetmID(Entity.Enum.SystemEnum.SystemID.典当, -1), "ID", "ProductName");// GetListFromDict("Product");
            ViewData["Source"] = EnumHelper.ToSelectListText<ApplyEnum.RegSource>();
            ViewData["DocType"] = EnumHelper.ToSelectListText<ApplyEnum.ProDocTypeName>();
            ViewData["Repayment"] = EnumHelper.ToSelectList<ApplyEnum.ProRepayment>();
            ViewData["HouType"] = EnumHelper.ToSelectList<ApplyEnum.ProHouType>();
            ViewData["BussType"] = EnumHelper.ToSelectList<ApplyEnum.ProBussType>();
            ViewData["DepID"] = new SelectList(serviceSysOrg.GetAllList(), "id", "orgname");
            ViewData["Status"] = EnumHelper.ToSelectList<ApplyEnum.ProStatus>();
          
        }

        public ActionResult LogerList(int id)
        {
            var list = serviceLogInfo.GetAllByProInfoID(id);
            return View(list);
        }


        [HttpPost]
        public string BackTo(int id)
        {
            string result = string.Empty;
            var model = new TB_APP_PROINFO();
            model.ID = (long)id;
            model.BACKBY = CurentUser.UserName;
            model.BACKREASON = "";
            try
            {
                bool isSumbit = false;

                isSumbit = serviceProInfo.AduitBackTo(model);

                if (isSumbit)
                { result = AjaxResultSucess; }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }
    }
}
